<?php
class Consultas_model extends CI_Model {
    public function __construct() {
        $this->load->database();
    }
    
    public function get_consultas() {
        $query = $this->db->query("SELECT *
                                    FROM
                                        consultas
                                    ORDER BY
                                        fecha DESC,
                                        idconsulta DESC");
        return $query->result_array();
    }
    
    public function set_consulta($datos) {
        $this->db->insert('consultas', $datos);
        return $this->db->insert_id();
    }
    
    public function get_consulta_por_id($idconsulta) {
        $query = $this->db->query("SELECT *
                            FROM    
                                consultas
                            WHERE
                                idconsulta = $idconsulta");
        return $query->row_array();
    }
    
    public function set_item_a_consulta($datos) {
        $this->db->insert('itemsconsulta', $datos);
    }
    
    public function get_items_de_una_consulta($idconsulta) {
        $query = $this->db->query("SELECT *
                            FROM    
                                itemsconsulta
                            WHERE
                                idconsulta = $idconsulta");
        return $query->result_array();
    }
    
    public function delete_item($iditem) {
        $this->db->delete('itemsconsulta', array('iditemsconsulta' => $iditem));
    }
    
    public function delete_consulta($idconsulta) {
        $this->db->delete('itemsconsulta', array('idconsulta' => $idconsulta));
        $this->db->delete('consultas', array('idconsulta' => $idconsulta));
    }
    
    public function get_item_por_id($iditem) {
        $query = $this->db->query("SELECT *
                                    FROM
                                        itemsconsulta
                                    WHERE
                                        iditemsconsulta = $iditem");
        return $query->row_array();
    }
    
    public function update_item($datos, $id) {
        $this->db->update('itemsconsulta', $datos, array('iditemsconsulta' => $id));
    }
    
    public function get_items_por_articulo($idarticulo) {
        $query = $this->db->query("SELECT ic.*
                                    FROM
                                        itemsconsulta ic,
                                        consultas c
                                    WHERE
                                        ic.idconsulta = c.idconsulta AND
                                        ic.idarticulo = $idarticulo
                                    ORDER BY
                                        c.fecha DESC");
        return $query->result_array();
    }
    
    public function get_cantidad_de_consultas() {
        $query = $this->db->query("SELECT COUNT(*) AS cant
                                    FROM
                                        consultas");
        return $query->row_array();
    }
    
    public function get_cantidad_de_consultas_por_articulo($idarticulo) {
        $query = $this->db->query("SELECT 
                                        COUNT(*) as cant
                                    FROM
                                        itemsconsulta
                                    WHERE
                                        idarticulo = '$idarticulo'");
        
        $res = $query->row_array();
        return $res['cant'];
    }
    
    public function get_items_por_proveedor($idproveedor) {
        $query = $this->db->query("SELECT c.fecha, c.moneda, ic.*
                                    FROM
                                        itemsconsulta ic,
                                        consultas c
                                    WHERE
                                        ic.idconsulta = c.idconsulta AND
                                        c.proveedor = $idproveedor
                                    ORDER BY
                                        c.fecha DESC");
        return $query->result_array();
    }
}
?>
